package com.mes.check.manager;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mes.check.manager.iface.IConcessionManager;
import com.mes.check.dao.iface.IDefectJudgeDetailDao;
import com.mes.check.domain.DefectJudgeDetail;
import com.mes.check.domain.Concession;
import com.mes.common.domain.BasicModel;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.SysUtil;
import com.mes.task.domain.OperationTask;
import com.mes.task.domain.WorkRecord;
import com.mes.task.manager.iface.IOperationTaskManager;

@Component
public class ConcessionManager extends BasicManager<Concession> implements IConcessionManager {
	private final static Log log = LogFactory.getLog(ConcessionManager.class);

	@Autowired
	IOperationTaskManager operationTaskManager;
	@Autowired
	IDefectJudgeDetailDao defectJudgeDetailDao;

	/**
	 * 将让步单设置为有效或者无效状态（设置有效性同时，反馈检验工序的完成进度）
	 * 
	 * @param repairUid
	 * @throws BaseBusinessException
	 */
	public void changeConcessionBillValidity(String repairUid, Boolean effective) throws Exception {
		try {
			Concession bill = this.getById(repairUid);
			bill.setIsInEffect(effective);
			this.update(bill);

			String taskuid = bill.getTaskUid();
			float repairQty = bill.getConcessionQty();
			if (effective == false) {
				repairQty = 0 - repairQty;

				List<DefectJudgeDetail> list = defectJudgeDetailDao
						.getDefectJudgeDetailByRelationBillUid(bill.getUuid());
				for (DefectJudgeDetail defectDetail : list) {
					defectDetail.setRelationBillUid(null);
					defectDetail.setRelationBillId(null);
					defectJudgeDetailDao.update(defectDetail);
				}
			}

			OperationTask operationTask = operationTaskManager.getById(taskuid);
			if ("202".endsWith(operationTask.getTaskType())) { // 如果是检验工序，则反馈完成进度
				List workRecList = new ArrayList(10);
				// 对反馈信息进行初始化；
				WorkRecord workRec = new WorkRecord();
				workRec.setTaskUid(taskuid);
				String sessionEmployeeId = SysUtil.getSessionEmployeeUid();
				workRec.setWorkerUid(sessionEmployeeId);
				// 获取更改的当前时间；
				java.util.Date now = new java.util.Date();
				java.sql.Timestamp time = new java.sql.Timestamp(now.getTime());
				workRec.setCreateTime(time);
				// 设置加工日期
				Date workDate = now;
				java.sql.Timestamp date = new java.sql.Timestamp(workDate.getTime());
				workRec.setWorkDate(date);
				workRec.setCompleteQty(repairQty);
				workRec.setCreatorUid(sessionEmployeeId);
				// workType＝1代表正常的加工记录；
				workRec.setWorkType(1);
				workRec.setObjectState(BasicModel.OBJECTSTATE_TOBEADDED); // 新增一条记录
				workRecList.add(workRec);

				operationTaskManager.feedbackTask(workRecList);
			}

		} catch (Exception e) {
			log.info(e.getStackTrace());
			throw e;
		}
	}

}
